package com.Babysleep.manage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import android.media.MediaPlayer;
import android.os.Message;
import android.widget.ImageButton;
import android.widget.TextView;

import com.Babysleep.R;
import com.Babysleep.Activity.MaMaShuo;
import com.Babysleep.Interface.ListenHandler;

public class ShiTing extends Listen {
	private MaMaShuo activity;
	private TextView view;
	private MyHandler handler;
	SimpleDateFormat df = new SimpleDateFormat("mm:ss");

	/**
	 * 试听
	 * 
	 */
	public ShiTing(MaMaShuo activity) {
		super(activity);
		this.activity = activity;
		// TODO Auto-generated constructor stub
	}

	public void init(ImageButton button,String name) {
		super.init(button,name);
		setLooping(false);
		df.setTimeZone(TimeZone.getTimeZone("GMT+0"));
		view = (TextView) activity.findViewById(R.id.counter);
		handler = new MyHandler(1000);
		setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				Pause();
			}
		});
	}

	public void UI() {
		view.setText(df.format(new Date(getCurrentPosition())));
	}

	public class MyHandler extends ListenHandler {

		public MyHandler(int delay) {
			super(delay);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				UI();
				if (need_send == true) {
					sendEmptyMessageDelayed(1, delay);
				}
				break;

			default:
				break;
			}
		}

	}

	public void Play() {
		// TODO Auto-generated method stub
		if (playing_started == false) {
			handler.start();
		}
		super.Play();

	}

	@Override
	public void Pause() {
		// TODO Auto-generated method stub
		super.Pause();// 播放控制
		handler.pause();// handler控制
	}

	@Override
	public void Restart() {
		// TODO Auto-generated method stub
		super.Restart();
		handler.restart();
	}

	@Override
	public void Stop() {
		// TODO Auto-generated method stub
		super.Stop();
		handler.stop();
	}

}
